package com.xray.netdisk.utils;

import com.xray.netdisk.pojo.FileItem;

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.List;

import static com.xray.netdisk.utils.FileUtils.*;

/**
 * 网络工具类V2
 */
public class NetworkUtilsV2 {

    /**
     * 通过用户id查询文件类别
     * @param userId
     * @return
     */
    public static List<FileItem> getFileList(Long userId){
        try(Socket socket = new Socket(IP,PORT)){
            DataInputStream input = new DataInputStream(socket.getInputStream());
            DataOutputStream output = new DataOutputStream(socket.getOutputStream());
            output.writeInt(TYPE_LIST);
            output.writeLong(userId);
            String json = input.readUTF();
            List<FileItem> fileItems = jsonToList(json);
            return fileItems;
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 上传文件到用户的目录中
     * @param userId
     * @param path
     */
    public static void uploadFile(Long userId,String path){
        try(Socket socket = new Socket(IP,PORT);
            DataOutputStream output = new DataOutputStream(socket.getOutputStream())){
            output.writeInt(TYPE_UPLOAD);
            File file = new File(path);
            if(file.exists()){
                output.writeUTF(file.getName());
                output.writeLong(userId);
                FileInputStream fileInputStream = new FileInputStream(file);
                FileUtils.io(fileInputStream,output);
                System.out.println("文件上传完毕");
            }else{
                output.writeUTF("error");
                System.out.println("文件不存在！");
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 下载文件
     * @param fileId 文件编号
     */
    public static void downloadFile(Long fileId){
        try(Socket socket = new Socket(IP,PORT);
            DataInputStream input = new DataInputStream(socket.getInputStream());
            DataOutputStream output = new DataOutputStream(socket.getOutputStream())){
            output.writeInt(TYPE_DOWNLOAD);
            output.writeLong(fileId);
            String result = input.readUTF();
            if("error".equals(result)){
                System.out.println("服务器没有此文件");
                return;
            }
            File downloadDir = new File(DOWNLOAD_DIR);
            if(!downloadDir.exists()){
                downloadDir.mkdir();
            }
            FileOutputStream fileOutputStream = new FileOutputStream(DOWNLOAD_DIR + "\\" + result);
            FileUtils.io(input,fileOutputStream);
            System.out.println("文件下载完毕");
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除文件
     * @param fileId 文件编号
     */
    public static void deleteFile(Long fileId){
        try(Socket socket = new Socket(IP,PORT);
            DataInputStream input = new DataInputStream(socket.getInputStream());
            DataOutputStream output = new DataOutputStream(socket.getOutputStream())){
            output.writeInt(TYPE_DELETE);
            output.writeLong(fileId);
            String result = input.readUTF();
            if("error".equals(result)){
                System.out.println("服务器没有此文件");
                return;
            }
            System.out.println(result+"文件删除完毕");
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 用户登录
     * @param username
     * @param password
     * @return
     */
    public static Long login(String username,String password){
        try(Socket socket = new Socket(IP,PORT);
            DataInputStream input = new DataInputStream(socket.getInputStream());
            DataOutputStream output = new DataOutputStream(socket.getOutputStream())){
            output.writeInt(TYPE_LOGIN);
            output.writeUTF(username);
            output.writeUTF(password);
            output.flush();
            Long id = input.readLong();
            return id;
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return -1L;
    }
}
